﻿using SlaughterItem.Common.DBContext;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using THE_WIND_RISES.Common.Domans.RBAC;
using WorkflowCore.Service.Persisted;
using WorkflowCore.Service.Steps;

namespace WorkflowCore.Service.Dto.Fundamentals
{
    public class GetWorkflowCoreUserData
    {
        public readonly WorkflowCoreDbcontext WorkflowDb;
        public readonly EFDBContext EfDb;

        public GetWorkflowCoreUserData(WorkflowCoreDbcontext workflowDb, EFDBContext efDb)
        {
            WorkflowDb = workflowDb;
            EfDb = efDb;
        }
        public dynamic ShoWorkflowCoreUserData()
        {
            var UserRoleData = GetUserRoleData();
            var UserData = WorkflowDb.WorkflowCoreUser.AsQueryable().ToList();
            List<PersonNode> WorkflowUserData= new List<PersonNode>();
            foreach (var user in UserData)
            {
                foreach (var node in UserRoleData)
                {
                    if (user.WorkflowCoreUserId == node.UserId)
                    {
                        PersonNode personNode = new PersonNode()
                        {
                            UserId = user.WorkflowCoreUserId,
                            UserName = node.UserName,
                            RoleId = node.RoleId,
                        };
                        foreach (var item in WorkflowUserData)
                        {
                            if (item.UserId != personNode.UserId)
                            {
                                WorkflowUserData.Add(personNode);
                            }
                        }
                    }
                    else
                    { 

                    }
                };
            };

            return WorkflowUserData;
        }
        public List<UserRoleDTO> GetUserRoleData()
        {
            List<UserRoleDTO> UserRoleData = (from a in EfDb.User.AsQueryable()
                                              join b in EfDb.UserRole.AsQueryable()
                                              on a.UserId equals b.UserId
                                              select new UserRoleDTO
                                              {
                                                  UserId=Convert.ToInt64(a.UserId),
                                                  UserName=a.UserName,
                                                  RoleId=Convert.ToInt64(b.RoleId)
                                              }
                ).ToList();
            return UserRoleData;
        }
    }
}
